﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Yugi.Client;
namespace Yugi_Gui
{
	/// <summary>
	/// Interaction logic for Health.xaml
	/// </summary>
	public partial class Health : UserControl, IInvoke
	{
        PlayerModel model;
        DispatcherTimer timer;
        int delta;
        int new_value;
        Storyboard str;
		public Health()
		{
			this.InitializeComponent();
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(Health_DataContextChanged);
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromTicks(1000);
            timer.Tick += new EventHandler(timer_Tick);
            
            healthpoint = 8000;
		}

        void timer_Tick(object sender, EventArgs e)
        {
            if(is_up)
            {
                HealthPoint += delta;
                if (healthpoint > new_value)
                {
                    HealthPoint = new_value;
                    timer.Stop();
                    str.Stop();
                }
            }
            else
            {
                HealthPoint += delta;
                if (healthpoint < new_value)
                {
                    HealthPoint = new_value;
                    timer.Stop();
                    str.Stop();
                }
            }
        }

        void Health_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            model = this.DataContext as PlayerModel;
            if (model == null) return;
            model.Health_Presenter = this;
        }
        int healthpoint;
        public int HealthPoint 
        {
            get
            {
                return healthpoint;
            }
            set
            {
                healthpoint = value;
                this.txt_health.Text = value.ToString();
            }
        }

        #region IInvoke Members

        public void Do(ClientMSG msg, params object[] obj)
        {
            switch (msg)
            {
                case ClientMSG.DoiDiem:
                    this.new_value = (int)obj[1];
                    delta = this.healthpoint < new_value ? 1 : -1;
                    if (this.healthpoint == new_value)
                        delta = 0;
                    if (delta > 0)
                    {
                        str = Resources["Up"] as Storyboard;
                        is_up = true;
                        str.Begin();
                        timer.Start();
                    }
                    else if( delta<0)
                    {
                        str = Resources["Down"] as Storyboard;
                        is_up = false;
                        str.Begin();
                        timer.Start();
                    }
                    break;
                case ClientMSG.Reset:
                    Reset();
                    break;
            }
        }

        public void Refresh()
        {

        }
        #endregion
        private bool is_up;
        public void Reset()
        {
            try
            {
                if (timer.IsEnabled)
                    timer.Stop();
                str.Stop();
            }
            catch (Exception ex)
            {
            }
            this.HealthPoint = 8000;
            model.Reset();
        }
    }
}